from datetime import datetime

from app import db


class Role(db.Model):
    __tablename__ = "roles"
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True)
    users = db.relationship('User', backref='role')
    created = db.Column(db.DateTime, default=datetime.now())
    update = db.Column(db.DateTime, default=datetime.now(), nullable=True)

    def __repr__(self):
        return '<Role %s>' % self.name

    def __str__(self):
        return self.name


class User(db.Model):
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey(Role.id))
    created = db.Column(db.DateTime, default=datetime.now())
    update = db.Column(db.DateTime, default=datetime.now(), nullable=True)

    def __repr__(self):
        return '<User %s>' % self.username

    def __str__(self):
        return self.username

class Teacher(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(20))
    sex = db.Column(db.String(2))
    create = db.Column(db.DateTime,default=datetime.now())
    classess = db.relationship('classes',backref='cls')

    def __repr__(self):
        return '<Teacher %s>' % self.name

    def __str__(self):
        return self.name


class classes(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    created = db.Column(db.DateTime, default=datetime.now())
    students = db.relationship('Student', backref='stu')
    teacher_id = db.Column(db.Integer, db.ForeignKey(Teacher.id))

    def __repr__(self):
        return '<classes %s>' % self.name

    def __str__(self):
        return self.name


class Student(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(30))
    sex = db.Column(db.String(2))
    age = db.Column(db.Integer)
    birth = db.Column(db.Date)
    class_id = db.Column(db.Integer, db.ForeignKey(classes.id), nullable=True)

    def __repr__(self):
        return '<Student %s>' % self.name

    def __str__(self):
        return self.name
